package com.bcgtgjyb.huanwen.customview.mylibrary;

import ohos.agp.animation.Animator;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.app.Context;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;
import ohos.global.icu.math.BigDecimal;

/**
 * Created by guohuanwen on 2015/10/5.
 */
public class WindowsLoad extends Component implements Component.DrawTask {
    float[] circleCentre;
    float[] start1;
    float[] start2;
    float[] start3;
    float x1,x2,x3,y1,y2,y3;
    EventHandler eventHandler = new EventHandler(EventRunner.getMainEventRunner());
    private float pi = (float) Math.PI;
    private Paint paint;
    private int mR;
    private float circleR;
    private DataAnimatorValue circleAnimator1;
    private DataAnimatorValue circleAnimator2;
    private DataAnimatorValue circleAnimator3;
    private boolean isInit = true;
    private SlowToQuick slowToQuick = new SlowToQuick();

    /**
     * 构造
     *
     * @param context 上下文
     * @param attrs 属性集
     */
    public WindowsLoad(Context context, AttrSet attrs) {
        super(context, attrs);
        addDrawTask(this);
        paint = new Paint();
        paint.setColor(new Color(Color.getIntColor("#FF4444")));
        mR = 10;
    }

    private void loading() {
        if (circleAnimator1 == null || !circleAnimator1.isRunning()) {
            circleAnimator1 = getCircleData(start1, circleCentre, 0);
            circleAnimator1.start();
        }
        if (circleAnimator2 == null || !circleAnimator2.isRunning()) {
            circleAnimator2 = getCircleData(start2, circleCentre, 30);
            circleAnimator2.start();
        }
        if (circleAnimator3 == null || !circleAnimator3.isRunning()) {
            circleAnimator3 = getCircleData(start3, circleCentre, 60);
            circleAnimator3.start();
        }
        eventHandler.postTask(new Runnable() {
            @Override
            public void run() {
                loading();
                invalidate();
            }
        }, 10);
    }

    private DataAnimatorValue getCircleData(float[] startCoordinate, float[] coordinate, int delay) {
        float startX = startCoordinate[0];
        float startY = startCoordinate[1];
        float coodX = coordinate[0];
        float coodY = coordinate[1];
        float ma;
        circleR = (float) Math.sqrt((startX - coodX) * (startX - coodX) + (startY - coodY) * (startY - coodY));
        float param = (float) (Math.abs(startY - coodY) / circleR);
        if (param < -1.0) {
            param = -1.0f;
        } else if (param > 1.0) {
            param = 1.0f;
        } else {}
        float am = (float) Math.asin(param);
        if (startX >= coodX && startY >= coodY) {
            ma = am;
        } else if (startX < coodX && startY >= coodY) {
            ma = pi - am;
        } else if (startX < coodX && startY < coodY) {
            ma = am + pi;
        } else {
            ma = 2 * pi - am;
        }
        DataAnimatorValue circleAnimator = new DataAnimatorValue();
        circleAnimator.setStart(ma);
        circleAnimator.setEnd((float) (ma + 2d * pi));
        circleAnimator.setDuration(1500);
        circleAnimator.setDelay(delay);
        circleAnimator.setCurve(slowToQuick);
        return circleAnimator;
    }

    // 获取同一个圆上，间隔固定角度的点坐标
    private float[] onCiecleCoordinate(float angle, float[] start, float[] center) {
        float x1 = start[0];
        float y1 = start[1];
        float x0 = center[0];
        float y0 = center[1];
        float sqrt = (float) Math.sqrt((x1 - x0) * (x1 - x0) + (y1 - y0) * (y1 - y0));
        float param = (float) (Math.abs(y1 - y0) / sqrt);
        float ma;
        if (param < -1.0) {
            param = -1.0f;
        } else if (param > 1.0) {
            param = 1.0f;
        } else {}
        float am = (float) Math.asin(param);
        if (x1 >= x0 && y1 >= y0) {
            ma = am;
        } else if (x1 < x0 && y1 >= y0) {
            ma = pi - am;
        } else if (x1 < x0 && y1 < y0) {
            ma = am + pi;
        } else {
            ma = 2 * pi - am;
        }

        float xx = (float) (center[0] + sqrt * Math.cos(ma + angle));
        float yy = (float) (center[1] + sqrt * Math.sin(ma + angle));
        return new float[]{xx, yy};
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        // 初始化
        if (isInit) {
            circleCentre = new float[]{(float) (getWidth() / 2d), (float) (getHeight() / 2d)};
            start1 = new float[]{((float)(double)getWidth() / 2), mR};
            start2 = onCiecleCoordinate(-0.5f, start1, circleCentre);
            start3 = onCiecleCoordinate(-0.5f, start2, circleCentre);
            isInit = false;
            loading();
        }

        // 第一个点初始位置
        if (!circleAnimator1.isRunning()) {
            canvas.drawCircle(start1[0], start1[1], mR, paint);
        }

        // 第二个点初始位置
        if (!circleAnimator2.isRunning()) {
            canvas.drawCircle(start2[0], start2[1], mR, paint);
        }

        // 第三个点初始位置
        if (!circleAnimator3.isRunning()) {
            canvas.drawCircle(start3[0], start3[1], mR, paint);
        }

        if (circleAnimator1.isRunning()) {
            x1 = (float) (circleCentre[0] + circleR * Math.cos((float) circleAnimator1.getAnimatedValue()));
            y1 = (float) (circleCentre[1] + circleR * Math.sin((float) circleAnimator1.getAnimatedValue()));
            canvas.drawCircle(x1, y1, mR, paint);
        }
        if (circleAnimator2.isRunning()) {
            x2 = (float) (circleCentre[0] + circleR * Math.cos((float) circleAnimator2.getAnimatedValue()));
            y2 = (float) (circleCentre[1] + circleR * Math.sin((float) circleAnimator2.getAnimatedValue()));

            canvas.drawCircle(x2, y2, mR, paint);
        }
        if (circleAnimator3.isRunning()) {
            x3 = (float) (circleCentre[0] + circleR * Math.cos((float) circleAnimator3.getAnimatedValue()));
            y3 = (float) (circleCentre[1] + circleR * Math.sin((float) circleAnimator3.getAnimatedValue()));
            canvas.drawCircle(x3, y3, mR, paint);
        }
        if (circleAnimator1.isRunning() || circleAnimator2.isRunning() || circleAnimator3.isRunning()) {
            invalidate();
        }
    }

    static class SlowToQuick implements Animator.TimelineCurve {
        @Override
        public float getCurvedTime(float v) {
            return v * v;
        }
    }
}
